//
// Copyright (C) 2011 - 2013  Steve Channell steve.channell@cepheis.com
//
// This file is part of Cephei.QL, an open-source library wrapper 
// arround QuantLib http://quantlib.org/
//
// Cephei.QL is open source software: you can redistribute it and/or modify it
// under the terms of the license.  You should have received a
// copy of the license along with this program; if not, please email
// <support@cepheis.com>. The license is also available online at
// <http://cepheis.com/license.htm>.
//
// This program is distributed in the hope that it will be useful, but WITHOUT
// ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
// FOR A PARTICULAR PURPOSE.  See the license for more details.
//
// Version 2.2 with QuantLib 1.2.1
//#include "stdafx.h"
#include "VarianceGammaProcess.h"
using namespace Cephei::QL::Experimental::Variancegamma;
#include <gen/QL/Termstructures/YieldTermStructure.h>
#include <gen/QL/Quote.h>
#include <gen/QL/Math/Array.h>
#include <gen/QL/StochasticProcess1D.h>
using namespace Cephei::QL::Termstructures;
using namespace Cephei::QL;
using namespace Cephei::QL::Math;
#define HANDLE
#undef ABSTRACT
#undef STRUCT
Cephei::QL::Experimental::Variancegamma::CVarianceGammaProcess::CVarianceGammaProcess (Cephei::QL::IQuote^ s0, Cephei::QL::Termstructures::IYieldTermStructure^ dividendYield, Cephei::QL::Termstructures::IYieldTermStructure^ riskFreeRate, Double sigma, Double nu, Double theta) : CStochasticProcess1D(CVarianceGammaProcess::typeid)
{
    CQuote^ _Cs0;
    CYieldTermStructure^ _CdividendYield;
    CYieldTermStructure^ _CriskFreeRate;
    try
    {
#ifdef HANDLE
        _phVarianceGammaProcess = NULL;
#endif
        _Cs0 = safe_cast<CQuote^> (s0);
        _Cs0->Lock();
        Handle<QuantLib::Quote>& _s0 = static_cast<Handle<QuantLib::Quote>&> (_Cs0->GetHandle ()); 
        _CdividendYield = safe_cast<CYieldTermStructure^> (dividendYield);
        _CdividendYield->Lock();
        Handle<QuantLib::YieldTermStructure>& _dividendYield = static_cast<Handle<QuantLib::YieldTermStructure>&> (_CdividendYield->GetHandle ()); 
        _CriskFreeRate = safe_cast<CYieldTermStructure^> (riskFreeRate);
        _CriskFreeRate->Lock();
        Handle<QuantLib::YieldTermStructure>& _riskFreeRate = static_cast<Handle<QuantLib::YieldTermStructure>&> (_CriskFreeRate->GetHandle ()); 
        QuantLib::Real _sigma = (QuantLib::Real)ValueHelper::Convert (sigma); //d
        QuantLib::Real _nu = (QuantLib::Real)ValueHelper::Convert (nu); //d
        QuantLib::Real _theta = (QuantLib::Real)ValueHelper::Convert (theta); //d
        _ppVarianceGammaProcess = new boost::shared_ptr<QuantLib::VarianceGammaProcess> (new QuantLib::VarianceGammaProcess ( _s0,  _dividendYield,  _riskFreeRate,  _sigma,  _nu,  _theta ));
        SetStochasticProcess1D (boost::dynamic_pointer_cast<QuantLib::StochasticProcess1D> (*_ppVarianceGammaProcess));
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown std::exception"));
	}
    finally
    {
        if (_Cs0 != nullptr) _Cs0->Unlock();
        if (_CdividendYield != nullptr) _CdividendYield->Unlock();
        if (_CriskFreeRate != nullptr) _CriskFreeRate->Unlock();
    }
}
Cephei::QL::Experimental::Variancegamma::CVarianceGammaProcess::CVarianceGammaProcess (boost::shared_ptr<QuantLib::VarianceGammaProcess>& childNative, Object^ owner) : CStochasticProcess1D(CVarianceGammaProcess::typeid)
{
#ifdef HANDLE
	_phVarianceGammaProcess = NULL;
#endif
	_ppVarianceGammaProcess = &childNative;
    _ppStochasticProcess1D = new boost::shared_ptr<QuantLib::StochasticProcess1D> (boost::dynamic_pointer_cast<QuantLib::StochasticProcess1D> (*_ppVarianceGammaProcess));
}
Cephei::QL::Experimental::Variancegamma::CVarianceGammaProcess::CVarianceGammaProcess (QuantLib::VarianceGammaProcess& childNative, Object^ owner) : CStochasticProcess1D(CVarianceGammaProcess::typeid)
{
#ifdef HANDLE
	_phVarianceGammaProcess = NULL;
#endif
	_ppVarianceGammaProcess = new boost::shared_ptr<QuantLib::VarianceGammaProcess> (&childNative);
    _ppStochasticProcess1D = new boost::shared_ptr<QuantLib::StochasticProcess1D> (boost::dynamic_pointer_cast<QuantLib::StochasticProcess1D> (*_ppVarianceGammaProcess));
    _VarianceGammaProcessOwner = owner;
    _StochasticProcess1DOwner = owner;
}

Cephei::QL::Experimental::Variancegamma::CVarianceGammaProcess::CVarianceGammaProcess (CVarianceGammaProcess^ copy) : CStochasticProcess1D(CVarianceGammaProcess::typeid)
{
#ifdef HANDLE
	_phVarianceGammaProcess = NULL;
#endif
	if (copy->HasNative() != NULL)
    {
		_ppVarianceGammaProcess = new boost::shared_ptr<QuantLib::VarianceGammaProcess> (copy->GetShared());
        _ppStochasticProcess1D = new boost::shared_ptr<QuantLib::StochasticProcess1D> (boost::dynamic_pointer_cast<QuantLib::StochasticProcess1D> (*_ppVarianceGammaProcess));
    }
}
Cephei::QL::Experimental::Variancegamma::CVarianceGammaProcess::CVarianceGammaProcess (PLATFORM::Type^ t) : CStochasticProcess1D(CVarianceGammaProcess::typeid)
{
#ifdef HANDLE
	_phVarianceGammaProcess = NULL;
#endif
	if (!t->IsSubclassOf(CVarianceGammaProcess::typeid))
		throw REFNEW Exception ("Invalid base-case init");
}
#ifdef HANDLE
Cephei::QL::Experimental::Variancegamma::CVarianceGammaProcess::CVarianceGammaProcess (QuantLib::Handle<QuantLib::VarianceGammaProcess>& childNative, Object^ owner)  : CStochasticProcess1D(CVarianceGammaProcess::typeid)
{
	_phVarianceGammaProcess = &childNative;
	_ppVarianceGammaProcess = &static_cast<boost::shared_ptr<QuantLib::VarianceGammaProcess>>(childNative.currentLink());
    _ppStochasticProcess1D = new boost::shared_ptr<QuantLib::StochasticProcess1D> (boost::dynamic_pointer_cast<QuantLib::StochasticProcess1D> (*_ppVarianceGammaProcess));
    _VarianceGammaProcessOwner = owner;
}
Cephei::QL::Experimental::Variancegamma::CVarianceGammaProcess::CVarianceGammaProcess (QuantLib::Handle<QuantLib::VarianceGammaProcess> childNative)  : CStochasticProcess1D(CVarianceGammaProcess::typeid)
{
	_phVarianceGammaProcess = &childNative;
	_ppVarianceGammaProcess = &static_cast<boost::shared_ptr<QuantLib::VarianceGammaProcess>>(childNative.currentLink());
    _ppStochasticProcess1D = new boost::shared_ptr<QuantLib::StochasticProcess1D> (boost::dynamic_pointer_cast<QuantLib::StochasticProcess1D> (*_ppVarianceGammaProcess));
}
#endif
#ifdef STRUCT
Cephei::QL::Experimental::Variancegamma::CVarianceGammaProcess::CVarianceGammaProcess (QuantLib::VarianceGammaProcess childNative)  : CStochasticProcess1D(CVarianceGammaProcess::typeid)
{
#ifdef HANDLE
	_phVarianceGammaProcess = NULL;
#endif
	_ppVarianceGammaProcess = new boost::shared_ptr<QuantLib::VarianceGammaProcess> (new QuantLib::VarianceGammaProcess (childNative));
    _ppStochasticProcess1D = new boost::shared_ptr<QuantLib::StochasticProcess1D> (boost::dynamic_pointer_cast<QuantLib::StochasticProcess1D> (*_ppVarianceGammaProcess));
}
#endif

Cephei::QL::Experimental::Variancegamma::CVarianceGammaProcess::~CVarianceGammaProcess ()
{
    if (_ppVarianceGammaProcess != NULL)
    {
	    delete _ppVarianceGammaProcess;
        _ppVarianceGammaProcess = NULL;
    }
}
Cephei::QL::Experimental::Variancegamma::CVarianceGammaProcess::!CVarianceGammaProcess ()
{
    if (_ppVarianceGammaProcess != NULL)
    {
	    delete _ppVarianceGammaProcess;
    }
}
QuantLib::VarianceGammaProcess& Cephei::QL::Experimental::Variancegamma::CVarianceGammaProcess::GetReference ()
{
    if (_ppVarianceGammaProcess == NULL) throw REFNEW NativeNullException ();
	return **_ppVarianceGammaProcess;
}
boost::shared_ptr<QuantLib::VarianceGammaProcess>& Cephei::QL::Experimental::Variancegamma::CVarianceGammaProcess::GetShared ()
{
    if (_ppVarianceGammaProcess == NULL) throw REFNEW NativeNullException ();
	return *_ppVarianceGammaProcess;
}
QuantLib::VarianceGammaProcess* Cephei::QL::Experimental::Variancegamma::CVarianceGammaProcess::GetPointer ()
{
    if (_ppVarianceGammaProcess == NULL) throw REFNEW NativeNullException ();
	return &**_ppVarianceGammaProcess;
}
#ifdef HANDLE
QuantLib::Handle<QuantLib::VarianceGammaProcess>& Cephei::QL::Experimental::Variancegamma::CVarianceGammaProcess::GetHandle ()
{
	if (_phVarianceGammaProcess == NULL)
	{
		_phVarianceGammaProcess = new Handle<QuantLib::VarianceGammaProcess> (*_ppVarianceGammaProcess);
	}
	return *_phVarianceGammaProcess;
}
#endif
bool Cephei::QL::Experimental::Variancegamma::CVarianceGammaProcess::HasNative () 
{
	return (_ppVarianceGammaProcess != NULL);
}

Double Cephei::QL::Experimental::Variancegamma::CVarianceGammaProcess::Diffusion (Double t, Double x)
{
    try
    {
        QuantLib::Time _t = (QuantLib::Time)ValueHelper::Convert (t); //a
        QuantLib::Real _x = (QuantLib::Real)ValueHelper::Convert (x); //a
    	QuantLib::Real _rv = (QuantLib::Real)(*_ppVarianceGammaProcess)->diffusion ( _t,  _x );   
        Double _nrv = (Double)ValueHelper::Convert (_rv); //c
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown std::exception"));
	}
    finally
    {
    }
}
Cephei::QL::Termstructures::IYieldTermStructure^ Cephei::QL::Experimental::Variancegamma::CVarianceGammaProcess::DividendYield::get ()
{
    try
    {
    	Handle<QuantLib::YieldTermStructure>& _rv = (Handle<QuantLib::YieldTermStructure>&)(*_ppVarianceGammaProcess)->dividendYield ( );   
        Cephei::QL::Termstructures::CYieldTermStructure^ _nrv = REFNEW Cephei::QL::Termstructures::CYieldTermStructure (_rv, this);
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown std::exception"));
	}
    finally
    {
    }
}
Double Cephei::QL::Experimental::Variancegamma::CVarianceGammaProcess::Drift (Double t, Double x)
{
    try
    {
        QuantLib::Time _t = (QuantLib::Time)ValueHelper::Convert (t); //a
        QuantLib::Real _x = (QuantLib::Real)ValueHelper::Convert (x); //a
    	QuantLib::Real _rv = (QuantLib::Real)(*_ppVarianceGammaProcess)->drift ( _t,  _x );   
        Double _nrv = (Double)ValueHelper::Convert (_rv); //c
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown std::exception"));
	}
    finally
    {
    }
}
Double Cephei::QL::Experimental::Variancegamma::CVarianceGammaProcess::Nu::get ()
{
    try
    {
    	QuantLib::Real _rv = (QuantLib::Real)(*_ppVarianceGammaProcess)->nu ( );   
        Double _nrv = (Double)ValueHelper::Convert (_rv); //c
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown std::exception"));
	}
    finally
    {
    }
}
Cephei::QL::Termstructures::IYieldTermStructure^ Cephei::QL::Experimental::Variancegamma::CVarianceGammaProcess::RiskFreeRate::get ()
{
    try
    {
    	Handle<QuantLib::YieldTermStructure>& _rv = (Handle<QuantLib::YieldTermStructure>&)(*_ppVarianceGammaProcess)->riskFreeRate ( );   
        Cephei::QL::Termstructures::CYieldTermStructure^ _nrv = REFNEW Cephei::QL::Termstructures::CYieldTermStructure (_rv, this);
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown std::exception"));
	}
    finally
    {
    }
}
Cephei::QL::IQuote^ Cephei::QL::Experimental::Variancegamma::CVarianceGammaProcess::S0::get ()
{
    try
    {
    	Handle<QuantLib::Quote>& _rv = (Handle<QuantLib::Quote>&)(*_ppVarianceGammaProcess)->s0 ( );   
        Cephei::QL::CQuote^ _nrv = REFNEW Cephei::QL::CQuote (_rv, this);
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown std::exception"));
	}
    finally
    {
    }
}
Double Cephei::QL::Experimental::Variancegamma::CVarianceGammaProcess::Sigma::get ()
{
    try
    {
    	QuantLib::Real _rv = (QuantLib::Real)(*_ppVarianceGammaProcess)->sigma ( );   
        Double _nrv = (Double)ValueHelper::Convert (_rv); //c
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown std::exception"));
	}
    finally
    {
    }
}
Double Cephei::QL::Experimental::Variancegamma::CVarianceGammaProcess::Theta::get ()
{
    try
    {
    	QuantLib::Real _rv = (QuantLib::Real)(*_ppVarianceGammaProcess)->theta ( );   
        Double _nrv = (Double)ValueHelper::Convert (_rv); //c
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown std::exception"));
	}
    finally
    {
    }
}
Double Cephei::QL::Experimental::Variancegamma::CVarianceGammaProcess::X0::get ()
{
    try
    {
    	QuantLib::Real _rv = (QuantLib::Real)(*_ppVarianceGammaProcess)->x0 ( );   
        Double _nrv = (Double)ValueHelper::Convert (_rv); //c
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown std::exception"));
	}
    finally
    {
    }
}
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Factory class

Cephei::QL::Experimental::Variancegamma::IVarianceGammaProcess^ Cephei::QL::Experimental::Variancegamma::CVarianceGammaProcess_Factory::Create (Cephei::QL::IQuote^ s0, Cephei::QL::Termstructures::IYieldTermStructure^ dividendYield, Cephei::QL::Termstructures::IYieldTermStructure^ riskFreeRate, Double sigma, Double nu, Double theta)
{
    return REFNEW CVarianceGammaProcess ( s0,  dividendYield,  riskFreeRate,  sigma,  nu,  theta);
}
